package com.shunbo.convenient.controller;

import com.shunbo.convenient.service.CouponDroolService;
import com.shunbo.convenient.service.CouponOrderService;
import com.shunbo.convenient.service.CouponService;
import com.shunbo.convenient.service.UserCouponService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.Coupon;
import com.shunbo.convenient.vo.CouponDrool;
import com.shunbo.convenient.vo.CouponOrder;
import com.shunbo.convenient.vo.UserCoupon;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author weizhuang
 * 夺宝
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Resource
    private CouponService couponService;

    @Resource
    private CouponOrderService couponOrderService;

    @Resource
    private CouponDroolService couponDroolService;

    @Resource
    private UserCouponService userCouponService;


    /**
     * 代金券列表
     *
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getCouponList(@RequestBody Coupon coupon) {
        try {
            List<Coupon> all = couponService.findAll(coupon);

            for (Coupon cou : all
                    ) {
                Integer count = 0;
                CouponOrder couponOrder = new CouponOrder();
                couponOrder.setCid(cou.getId());
                List<CouponOrder> all1 = couponOrderService.findAll(couponOrder);
                for (CouponOrder coupono:all1
                ) {
                    count+= coupono.getNum();
                }
                cou.setNum(count);
                CouponDrool couponDrool = new CouponDrool();
                couponDrool.setCid(cou.getId());
                List<CouponDrool> all2 = couponDroolService.findAll(couponDrool);
                cou.setCouponDrools(all2);
            }

            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }


    /**
     * 代金券
     *
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getCouponById(@RequestBody Coupon coupon) {
        try {
            Coupon cou = couponService.findById(coupon.getId() + "");
            CouponOrder couponOrder = new CouponOrder();
            couponOrder.setCid(cou.getId());
            List<CouponOrder> all1 = couponOrderService.findAll(couponOrder);
            cou.setNum(all1.size());
            CouponDrool couponDrool = new CouponDrool();
            couponDrool.setCid(cou.getId());
            List<CouponDrool> all = couponDroolService.findAll(couponDrool);
            cou.setCouponDrools(all);
            return ConvenientResult.build(200, "成功", cou);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 我的代金券
     *
     * @return Result结果集
     */
    @RequestMapping("/findByUserId")
    public ConvenientResult getCouponByUserId(@RequestBody CouponOrder couponOrderRequest) {
        try {
            List<CouponOrder> all2 = couponOrderService.findAll(couponOrderRequest);
            for (CouponOrder couponOrders : all2
                    ) {
                Coupon cou = couponService.findById(couponOrders.getCid() + "");
                CouponOrder couponOrder = new CouponOrder();
                couponOrder.setCid(cou.getId());
                List<CouponOrder> all1 = couponOrderService.findAll(couponOrder);
                cou.setNum(all1.size());
                CouponDrool couponDrool = new CouponDrool();
                couponDrool.setCid(cou.getId());
                List<CouponDrool> all = couponDroolService.findAll(couponDrool);
                cou.setCouponDrools(all);

                couponOrders.setCoupon(cou);
            }

            return ConvenientResult.build(200, "成功", all2);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 我已有的代金券
     *
     * @return Result结果集
     */
    @RequestMapping("/findByCoupon")
    public ConvenientResult findByCoupon(@RequestBody UserCoupon userCoupon) {
        try {

            List<UserCoupon> all = userCouponService.findAll(userCoupon);

            for (UserCoupon userCoupon1 : all
                    ) {
                Coupon byId = couponService.findById(userCoupon1.getCid() + "");
                userCoupon1.setCoupon(byId);
            }
            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 新增代金券
     *
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveCoupon(@RequestBody Coupon coupon) {
        try {

            couponService.save(coupon);
            List<CouponDrool> couponDrools = coupon.getCouponDrools();
            for (CouponDrool cou : couponDrools
                    ) {
                cou.setCid(coupon.getId());
                couponDroolService.save(cou);
            }
            return ConvenientResult.build(200, "成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新代金券
     *
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateCoupon(@RequestBody Coupon coupon) {
        try {

            couponService.update(coupon);
            List<CouponDrool> couponDrools = coupon.getCouponDrools();
            for (CouponDrool cou : couponDrools
                    ) {
                couponDroolService.update(cou);
            }
            return ConvenientResult.build(200, "成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 删除代金券
     *
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteCoupon(@RequestBody Coupon coupon) {
        try {

            couponService.delete(coupon.getId() + "");
            List<CouponDrool> couponDrools = coupon.getCouponDrools();
            for (CouponDrool cou : couponDrools
                    ) {
                couponDroolService.delete(cou.getId() + "");
            }
            return ConvenientResult.build(200, "成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }


}
